package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.server.HttpServerRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.ReservationDto;
import com.zzyl.dto.ReservationQueryDto;
import com.zzyl.entity.Member;
import com.zzyl.entity.Reservation;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.ThreadLocalUtil;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 分页查找预约
     */
    @Override
    public PageBean<ReservationVo> findByPage(ReservationQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

        Long userId = ThreadLocalUtil.get();
        Member member = memberMapper.selectById(userId);
        if(!ObjectUtil.isEmpty(member)){
            dto.setUserId(userId);
        }
        Page<Reservation> page = reservationMapper.findByPage(dto);
        return PageBean.of(page, ReservationVo.class);


    }

    @Override
    public Integer getNum() {
        // 当天的开始时间
        LocalDateTime start = LocalDateTime.now().with(LocalTime.MIN);
        // 当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        int cancelledCount = reservationMapper.getCancelledCount(start, end, ThreadLocalUtil.get());
        return cancelledCount;


    }

    @Override
    public List<TimeCountVo> getCount(Long time) {
        // 当天的开始时间
        LocalDateTime start = LocalDateTime.now().with(LocalTime.MIN);
        // 当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);

        List<TimeCountVo> timeCountVos = reservationMapper.countByTimeRange(start,end);
        return timeCountVos;
    }

    /**
     * 添加预约
     * @param dto
     */
    @Override
    public void addReservation(ReservationDto dto) {
        //将DTO转换为POJo
        Reservation reservation= BeanUtil.toBean(dto,Reservation.class);
        reservation.setStatus(0);//0：待报道，1：已完成，2：取消，3：过期
        reservationMapper.insert(reservation);
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {
        Reservation reservation=Reservation.builder()
                .id(id)
                .status(2)//日待报道1已完成2取消3过期
                .build();
        reservationMapper.update(reservation);
    }
}

